package com.example.webrtcrooma;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;

import androidx.appcompat.app.AppCompatActivity;

import com.example.webrtcrooma.util.PermissionUtil;
import com.example.webrtcrooma.util.Utils;
import com.example.webrtcrooma.webrtc.WebRtcManager;

import org.webrtc.EglBase;
import org.webrtc.MediaStream;
import org.webrtc.RendererCommon;
import org.webrtc.SurfaceViewRenderer;
import org.webrtc.VideoTrack;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ChatRoomActivity extends AppCompatActivity {
    private int mScreenWidth;
    private FrameLayout frameLayout;
    private WebRtcManager webRtcManager;
    private EglBase eglBase;
    private VideoTrack localVideoTrack;
    private List<String> person = new ArrayList<>();
    private Map<String, SurfaceViewRenderer> videoViews = new HashMap<>();
    public static void openActivity(Activity activity){
        Intent intent = new Intent(activity,ChatRoomActivity.class);
        activity.startActivity(intent);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON|
                WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG|
                WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED|
                WindowManager.LayoutParams.PARCELABLE_WRITE_RETURN_VALUE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat_room);
        initView();
    }

    private void initView() {
        frameLayout = findViewById(R.id.frameLayout);
        eglBase = EglBase.create();
        webRtcManager = WebRtcManager.getInstance();
        if(!PermissionUtil.isNeedRequestPermission(this)){
            webRtcManager.joinRoom(this,eglBase);

        }
    }
    public void onSetLocalStream(MediaStream mediaStream, String userId){
        if(mediaStream.videoTracks.size()>0){
            localVideoTrack = mediaStream.videoTracks.get(0);

        }
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                addView(userId, mediaStream);
            }
        });
    }
    public void onSetRemoteStream(MediaStream mediaStream, String userId){
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                addView(userId, mediaStream);
            }
        });
    }

    private void addView(String userId, MediaStream mediaStream) {
        SurfaceViewRenderer surfaceViewRenderer = new SurfaceViewRenderer(this);
        //webrtc自带,不考虑渲染问题
        surfaceViewRenderer.init(eglBase.getEglBaseContext(), null);
        surfaceViewRenderer.setScalingType(RendererCommon.ScalingType.SCALE_ASPECT_FIT);
        surfaceViewRenderer.setMirror(true);
        if(mediaStream.videoTracks.size()>0){
            mediaStream.videoTracks.get(0).addSink(surfaceViewRenderer);

        }
        videoViews.put(userId, surfaceViewRenderer);
        person.add(userId);
        frameLayout.addView(surfaceViewRenderer);
        int size = videoViews.size();
        for (int i = 0; i < size; i++){
            String peerId = person.get(i);
            SurfaceViewRenderer renderer = videoViews.get(peerId);
            if (renderer != null){
                FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                layoutParams.height = getWidth(size);
                layoutParams.width = getWidth(size);
                layoutParams.leftMargin = getX(size, i);
                layoutParams.topMargin = getY(size, i);
                renderer.setLayoutParams(layoutParams);
            }
        }
    }
    private int getWidth(int size) {
        if (size <= 4) {
            return mScreenWidth / 2;
        } else if (size <= 9) {
            return mScreenWidth / 3;
        }
        return mScreenWidth / 3;
    }

    private int getX(int size, int index) {
        if (size <= 4) {
            if (size == 3 && index == 2) {
                return mScreenWidth / 4;
            }
            return (index % 2) * mScreenWidth / 2;
        } else if (size <= 9) {
            if (size == 5) {
                if (index == 3) {
                    return mScreenWidth / 6;
                }
                if (index == 4) {
                    return mScreenWidth / 2;
                }
            }

            if (size == 7 && index == 6) {
                return mScreenWidth / 3;
            }

            if (size == 8) {
                if (index == 6) {
                    return mScreenWidth / 6;
                }
                if (index == 7) {
                    return mScreenWidth / 2;
                }
            }
            return (index % 3) * mScreenWidth / 3;
        }
        return 0;
    }

    private int getY(int size, int index) {
        if (size < 3) {
            return mScreenWidth / 4;
        } else if (size < 5) {
            if (index < 2) {
                return 0;
            } else {
                return mScreenWidth / 2;
            }
        } else if (size < 7) {
            if (index < 3) {
                return mScreenWidth / 2 - (mScreenWidth / 3);
            } else {
                return mScreenWidth / 2;
            }
        } else if (size <= 9) {
            if (index < 3) {
                return 0;
            } else if (index < 6) {
                return mScreenWidth / 3;
            } else {
                return mScreenWidth / 3 * 2;
            }

        }
        return 0;
    }

    public void onCloseWithId(String userId) {
        removeView(userId);
    }

    private void removeView(String userId) {

    }
}